﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQData;
using VQWEB.administrator.sources;
using System.Data;
using VQWEB.common;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for CoursesHandler
    /// </summary>
    public class eng_CourseHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Course     
            string add_course = context.Request["add_course"];
            if (!string.IsNullOrEmpty(add_course))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               var obj = bussiness.InsertOrUpdate<eng_course>(add_course);
               context.Response.Write("Success Full");
            }

            string view_course = context.Request["view_course"];
            if (!string.IsNullOrEmpty(view_course))
            {
                string Id = context.Request["Id"];
                RenderSingle(context, Convert.ToInt32(Id));
            }

            string list_course = context.Request["list_course"];
            if(!string.IsNullOrEmpty(list_course)){
                RenderList(context);               
            }
            string delete_course = context.Request["delete_course"];
            if (!string.IsNullOrEmpty(delete_course)) {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               bussiness.Delete<eng_course>(delete_course);
                RenderList(context);
            }
            string lockunlock_course = context.Request["lockunlock_course"];
            if (!string.IsNullOrEmpty(lockunlock_course)) {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<hn_hedaotao>(lockunlock_course, "course_active");
                RenderList(context);
            }
            #endregion
        }

        private void RenderSingle(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_course>(
               c => c.course_id.Equals(Id),
               c => new
               {
                   id = c.course_id,
                   code = c.course_code,
                   title = c.course_title,
                   active = c.course_active
               }
               );
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }

        private void RenderList(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_course>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.course_id,
                    code = c.course_code,
                    title = c.course_title,                   
                    active = c.course_active
                }
                );

            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
    
}

